#encoding=utf-8
import sys
import os
from MySQLdb import *
reload(sys)
sys.setdefaultencoding('utf-8')

class mysqlClass:

    def __init__(self,config):
        self.config={
            'local':config
        }

    def run(self,sql,flag,config='local'):
        data=self.config[config]
        conn=connect(host=data['host'],port=data['port'],db=data['db'],user=data['user'],passwd=data['passwd'],charset=data['charset'])
        cursor=conn.cursor(cursorclass=cursors.DictCursor)
        result=cursor.execute(sql)
        if flag=='execute':
            conn.commit()
            result=cursor.lastrowid
        elif flag=='fetchall':
            result=cursor.fetchall()
        elif flag=='fetchone':
            result=cursor.fetchone()
        cursor.close()
        conn.close()
        return result

    def execute(self,sql,config='local'):
        return self.run(sql,'execute',config)

    def fetchall(self,sql,config='local'):
        return self.run(sql,'fetchall',config)

    def fetchone(self,sql,config='local'):
        return self.run(sql,'fetchone',config)

    def escape_string(self,string):
        return escape_string(string)
